package com.tide.crm.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.tide.crm.dao.ConvergeRoleDao;
import com.tide.crm.dto.ConvergeRoleDto;
import com.tide.crm.model.ConvergeRole;
import com.tide.crm.service.ConvergeRoleService;
import com.tide.crm.utils.SnowFlakeIdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


@Service("convergeRoleService")
public class ConvergeRoleServiceImpl implements ConvergeRoleService {

	private static final Logger log = LoggerFactory.getLogger("adminLogger");

	@Resource(name="convergeRoleDao")
	private ConvergeRoleDao convergeRoleDao;
	
	@Override
	public ConvergeRole getConvergeRoleById(Long id) {
		// TODO Auto-generated method stub
		return convergeRoleDao.getById(id);
	}

	@Override
	@Transactional
	public boolean saveConvergeRole(ConvergeRoleDto roleDto) {
		// TODO Auto-generated method stub
		boolean result ;
		ConvergeRole role = new ConvergeRole();
		BeanUtils.copyProperties(roleDto, role);
		List<Long> permissionIds = roleDto.getPermissionIds();
		permissionIds.remove(0L);

		if (role.getId() != null) {// 修改
			result = updateRole(role, permissionIds);
		} else {// 新增
			result = saveRole(role, permissionIds);
		}
		return result;
	}

	private boolean  saveRole(ConvergeRole role, List<Long> permissionIds) {
		ConvergeRole r = convergeRoleDao.getRole(role.getRoleName());
		if (r != null) {
			throw new IllegalArgumentException(role.getRoleName() + "已存在");
		}
		SnowFlakeIdGenerator worker2 = new SnowFlakeIdGenerator(2);
		role.setId(worker2.nextId());
		int result = convergeRoleDao.save(role);
		if (!CollectionUtils.isEmpty(permissionIds)) {
			convergeRoleDao.saveRolePermission(role.getId(), permissionIds);
		}
		
		
		log.debug("新增角色{}", role.getRoleName());
		if(result > 0) {
			return true;
		}
		return false;
	}

	private boolean updateRole(ConvergeRole role, List<Long> permissionIds) {
		ConvergeRole r = convergeRoleDao.getRole(role.getRoleName());
//		if (r != null && (r.getId() != role.getId())) {
//			throw new IllegalArgumentException(role.getRoleName() + "已存在");
//		}
		int result = convergeRoleDao.update(role);
		convergeRoleDao.deleteRolePermission(role.getId());
		if (!CollectionUtils.isEmpty(permissionIds)) {
			convergeRoleDao.saveRolePermission(role.getId(), permissionIds);
		}
		log.debug("修改角色{}", role.getRoleName());
		if(result > 0) {
			return true;
		}
		return false;
	}
	
	@Override
	public boolean updateConvergeRole(ConvergeRole role) {
		// TODO Auto-generated method stub
		role.setModifyDate(new Date());
		int result = convergeRoleDao.update(role);
		if(result > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean saveConvergeRole(ConvergeRole role) {
		// TODO Auto-generated method stub
		return false;
	}

}
